package com.tool.payment;

import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.tool.payment.exception.SDKRuntimeException;
import com.tool.payment.util.CommonUtil;
import com.tool.payment.util.MD5SignUtil;
import com.tool.payment.util.ToolProperties;

public class PaymentConfig {
	/**微信生成预订单链接*/
	private static final String WEIXIN_PREORDER_URL = "https://api.mch.weixin.qq.com/pay/unifiedorder";
	/**微信退款链接**/
	private static final String WEIXIN_REFUND_URL = "https://api.mch.weixin.qq.com/secapi/pay/refund";
	/**微信退款查询**/
	private static final String WEIXIN_REFUND_QUERY_URL = "https://api.mch.weixin.qq.com/pay/refundquery";
	
	private static final String WEIXIN_DOWNLOAD_BILL_URL = "https://api.mch.weixin.qq.com/pay/downloadbill";
	
	private static PaymentConfig paymentConfig = new PaymentConfig();
	
	private static ToolProperties prop = new ToolProperties("payment.properties");
	
	private static final String API_WEIXIN_APPID = "api.weixin.appid";
	private static final String API_WEIXIN_MCHID = "api.weixin.mchid";
	private static final String API_WEIXIN_SIGNTYPE = "api.weixin.signtype";
	private static final String API_WEIXIN_BODY = "api.weixin.body";
	private static final String API_WEIXIN_PARTNERKEY = "api.weixin.partnerkey";
	private static final String API_WEIXIN_CERTLOCALPATH = "api.weixin.certlocalpath";
	private static final String API_WEIXIN_CERTPASSWORD = "api.weixin.certpassword";
	private static final String BILL_PATH = "bill.path";
	
	private static final String ALIPAY_PARTNER = "alipay.partner";
	private static final String ALIPAY_KEY  = "alipay.key";
	private static final String ALIPAY_SELLER_EMAIL = "alipay.selleremail";
	private static final String ALIPAY_SELLER_NAME = "alipay.sellername";
	
	private String alipayKey;
	private String alipaySellerEmail;
	private String alipaySellerName;
	private String alipayPartner;
	
	private String apiWeixinCertLocalPath;
	private String apiWeixinCertPassword;
	private String apiWeixinAppId;
	private String apiWeixinMchId;
	private String apiWeixinBody;
	private String signType;
	private String apiWeixinPartnerkey;
	
	private PaymentConfig(){}
	
	public static PaymentConfig getInstance() {
		return paymentConfig;
	}
	
	/**商户的私钥**/
	public String getAlipayKey() {
		return prop.getString(ALIPAY_KEY,alipayKey);
	}
	/**卖家账户**/
	public String getAlipaySellerEmail() {
		return prop.getString(ALIPAY_SELLER_EMAIL,alipaySellerEmail);
	}
	/**账户名**/
	public String getAlipaySellerName() {
		return prop.getString(ALIPAY_SELLER_NAME,alipaySellerName);
	}
	/** 合作身份者ID，以2088开头由16位纯数字组成的字符串**/
	public String getAlipayPartner() {
		return prop.getString(ALIPAY_PARTNER,alipayPartner);
	}
	
	public String getAlipayInputCharset() {
		return "utf-8";
	}
	
	public String getAlipaySignType () {
		return "MD5";
	}
	
	/**
	 * HTTPS证书的本地路径
	 * @return
	 */
	public String getApiWeixinCertLocalPath(){
		return prop.getString(API_WEIXIN_CERTLOCALPATH,apiWeixinCertLocalPath);
	}
	/**
	 * HTTPS证书密码，默认密码等于商户号MCHID
	 * @return
	 */
	public String getApiWeixinCertPassword(){
		return prop.getString(API_WEIXIN_CERTPASSWORD,apiWeixinCertPassword);
	}
	/**
	 * 公众号id
	 * @return
	 */
	public String getApiWeixinAppId() {
		return prop.getString(API_WEIXIN_APPID,apiWeixinAppId);
	}
	/**
	 * 商户号mch_id
	 * @return
	 */
	public String getApiWeixinMchId() {
		return prop.getString(API_WEIXIN_MCHID,apiWeixinMchId);
	}
	/**
	 * 商品描述
	 * @return
	 */
	public String getApiWeixinBody() {
		return prop.getString(API_WEIXIN_BODY,apiWeixinBody);
	}
	/**
	 * 签名类型
	 * @return
	 */
	public String getSignType() {
		return prop.getString(API_WEIXIN_SIGNTYPE,signType);
	}
	/**
	 * 微信预订单链接
	 * @return
	 */
	public String getWeixinPreorderUrl(){
		return WEIXIN_PREORDER_URL;
	}
	/**
	 * 微信预订单链接
	 * @return
	 */
	public String getWeixinRefundUrl(){
		return WEIXIN_REFUND_URL;
	}
	
	/**
	 * 微信退款查询
	 * @return
	 */
	public String getWeixinRefundQueryUrl() {
		return WEIXIN_REFUND_QUERY_URL;
	}
	/**
	 * 下载对账单
	 * @return
	 */
	public String getWeixinDownLoadBillUrl() {
		return WEIXIN_DOWNLOAD_BILL_URL;
	}
	
	/**
	 * 账单保存路径
	 * @return
	 */
	public String getBillPath() {
		return BILL_PATH;
	}
	
	/**
	 * 生成随机数
	 * @return
	 */
	public String getApiWeixinNonceStr() {
		return CommonUtil.CreateNoncestr();
	}
	/**
	 * 获得时间戳
	 * @return
	 */
	public String getApiWeixinTimeStamp() {
		long seconds = System.currentTimeMillis()/1000;
		return String.valueOf(seconds);
	}
	/**
	 * 生成签名秘钥
	 * @param parameters
	 * @return
	 * @throws SDKRuntimeException
	 */
	public String getSignStr(Map<String, String> parameters) throws SDKRuntimeException {
		String partnerKey = prop.getProperty(API_WEIXIN_PARTNERKEY,apiWeixinPartnerkey);
		if (StringUtils.isEmpty(partnerKey)) {
			throw new SDKRuntimeException("密钥不能为空！");
		}
		String unSignParaString = CommonUtil.FormatBizQueryParaMap(parameters,
				false);
		return  MD5SignUtil.Sign(unSignParaString, partnerKey).toUpperCase();
	}

	public void setAlipayKey(String alipayKey) {
		this.alipayKey = alipayKey;
	}

	public void setAlipaySellerEmail(String alipaySellerEmail) {
		this.alipaySellerEmail = alipaySellerEmail;
	}

	public void setAlipaySellerName(String alipaySellerName) {
		this.alipaySellerName = alipaySellerName;
	}

	public void setAlipayPartner(String alipayPartner) {
		this.alipayPartner = alipayPartner;
	}

	public void setApiWeixinCertLocalPath(String apiWeixinCertLocalPath) {
		this.apiWeixinCertLocalPath = apiWeixinCertLocalPath;
	}

	public void setApiWeixinCertPassword(String apiWeixinCertPassword) {
		this.apiWeixinCertPassword = apiWeixinCertPassword;
	}

	public void setApiWeixinAppId(String apiWeixinAppId) {
		this.apiWeixinAppId = apiWeixinAppId;
	}

	public void setApiWeixinMchId(String apiWeixinMchId) {
		this.apiWeixinMchId = apiWeixinMchId;
	}

	public void setApiWeixinBody(String apiWeixinBody) {
		this.apiWeixinBody = apiWeixinBody;
	}

	public void setSignType(String signType) {
		this.signType = signType;
	}

	public void setApiWeixinPartnerkey(String apiWeixinPartnerkey) {
		this.apiWeixinPartnerkey = apiWeixinPartnerkey;
	}
}
